<?php

namespace App\Http\Controllers\Teacher;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use App\Model\Users;

class RegisterController extends Controller
{
	use RegistersUsers;
    protected $redirectTo = '/Teacher/index'; //成功后跳转 

    //自定义获取视图地址
    public function showRegistrationForm()
    {
        return view('teacher.register');
    }

    //数据验证 
    protected function validator(array $data)
    {
        $errMsg = [
            'username.required' => '用户名不能为空！',
            'username.regex' => '用户名不合法！',
            'username.unique' => '用户名已经存在！',
            'password.required' => '密码不能为空！',
            'password.min' => '密码不能小于6位！',
            'password.confirmed' => '两次密码不一致！',
            'code.required' => '验证码不能为空！',
            'code.captcha' => '验证码错误！',
            'email.required' => '邮箱不能为空！',
            'email.email' => '非邮箱格式！',
            'email.unique' => '邮箱已经存在了！',
        ];
        return Validator::make($data, [
            'username' => 'required|regex:/^[A-Za-z]+[0-9]*$/|string|max:12|unique:users',
            'email' => 'required|string|email|max:30|unique:users',
            'password' => 'required|string|min:6|confirmed',
        ],$errMsg);
    }

    //开始插入数据
    protected function create(array $data)
    {
        return Users::create([
            'username' => $data['username'],
            'nick' => '无名',
            'email' => $data['email'],
            'is_admin' => 0,
            'is_teacher' => 1,
            'password' => bcrypt($data['password']),
        ]);
    }
}
